package frame;

import com.alibaba.fastjson2.JSONObject;
import common.User;
import function.SearchUser;
import function.SendMessage;
import global.Const;
import global.PublciVariable;
import other.DButton;
import panel.common.BackGround;
import panel.common.SearchField;
import panel.common.UserInfo;
import windows.LoadingDialog;
import windows.MessageDialog;

import javax.swing.*;
import java.awt.*;

public class FindUser extends JFrame {
    public User user = new User();
    public UserInfo userInfo = new UserInfo();
    public SearchField searchField = new SearchField("用户名");
    public LoadingDialog loading = new LoadingDialog();
    public MessageDialog messageDialog = new MessageDialog();
    public DButton makeFriend = new DButton(new ImageIcon("src/main/resources/icon/home/makeFriend.png"));
    public DButton chatButton = new DButton(new ImageIcon("src/main/resources/icon/home/chat.png"));

    public FindUser() {
        init();
    }

    private void init() {
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setSize(Const.WINDOW_WIDTH + Const.WIDTH_ERROE, Const.WINDOW_HEIGHT + Const.HEIGHT_ERROE + 64);
        setLocationRelativeTo(null);
        setAlwaysOnTop(true);
        setTitle("查找用户");
        setResizable(false);
        setBackground(Color.white);
        setLayout(null);

        // 加载框
        loading.getWindow().setLocation(0, 64);
        // 提示框
        messageDialog.getWindow().setLocation(0, 64);

        // 搜索框
        searchField.setBounds(-5, 0, getWidth() - Const.WIDTH_ERROE + 10, 64);
        searchField.searchButton.addActionListener(e -> searchUser());

        // 私聊按钮
        chatButton.setLocation(286, 188);
        chatButton.addActionListener(e -> openChat());
        chatButton.setVisible(false);

        // 添加好友按钮
        makeFriend.setLocation(50, 188);
        makeFriend.addActionListener(e -> makeFriend());
        makeFriend.setVisible(false);

        // 用户信息
        userInfo.setLocation(0, 64);

        // 添加组件
        add(loading.getWindow());
        add(messageDialog.getWindow());
        add(makeFriend);
        add(chatButton);
        add(searchField);
        add(userInfo);
        add(new BackGround("src/main/resources/image/home/searchUser.jpg", getWidth(), getHeight()));

        // 启动
        setVisible(true);
    }

    private void makeFriend() {
        SendMessage.sendApply(user.username, PublciVariable.username);
        messageDialog.open("已发送好友请求");
    }

    private void openChat() {
        if (user.allowStrangerChat) {
            new Chat(user);
            dispose();
        }
        else
            messageDialog.open("对方拒绝和陌生人聊天");
    }

    private void searchUser() {
        userInfo.close();
        if (searchField.getText().isEmpty()) {
            makeFriend.setVisible(false);
            chatButton.setVisible(false);
            messageDialog.open("请求错误", "请先输入用户名在查找");
            return;
        }
        if(searchField.getText().equals(PublciVariable.username)){
            makeFriend.setVisible(false);
            chatButton.setVisible(false);
            messageDialog.open("不能查找自己");
            return;
        }
        SwingWorker worker = new SwingWorker<JSONObject, Void>() {
            @Override
            protected JSONObject doInBackground() {
                loading.open(); // 打开加载窗口
                return SearchUser.get(searchField.getText());
            }

            @Override
            protected void done() {
                try {
                    JSONObject reserve = get();
                    if (reserve.getInteger("code") == 0) {// 查找成功
                        findSucceed(reserve);
                    } else {// 查找失败
                        messageDialog.open(reserve.getString("title"), reserve.getString("message"));
                        makeFriend.setVisible(false);
                        chatButton.setVisible(false);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    loading.close();
                }
            }
        };
        worker.execute();
    }

    public void findSucceed(JSONObject userJson) {
        user.username = userJson.getString("username");
        user.sign = userJson.getString("sign");
        user.allowStrangerChat = userJson.getBoolean("allowStrangerChat");
        userInfo.open(user.username, user.sign, user.allowStrangerChat);
        chatButton.setVisible(true);
        makeFriend.setVisible(true);
    }
}
